* local inequality in BCN descriptive
********************************************************************************
* prelims
********************************************************************************
{
clear all
set more off
cap log close
}
********************************************************************************
* inequality estimates dwelling value and space
********************************************************************************
{

* price estimates
use "$data/int/PredPriceForest_08_900.dta", clear

* add square meters and yearconst
merge 1:1 PlotCode PropOrder PropOrder2 using "$data/int/08_900.dta", keepusing(PropSqm YearConst)
drop if _merge==2
drop _merge

* add admin boundaries info
local varskeep CMUN CDIS CSEC BARRI ZIPCode
merge m:1 PlotCode using "$data/int/catastro_plot_chars.dta", keepusing(`varskeep')
drop if _merge==2
drop _merge

* drop variables defined at local level
drop PlotCode PropOrder PropOrder2

* tract identifier
g CUSEC = CMUN + CDIS + CSEC

* ineq at given level of aggregation/year
forval year = 2019(-1)2009 {

foreach agglevel in CMUN CDIS CUSEC BARRI ZIPCode {

	* retrieve list
	levelsof `agglevel', local(list)

	foreach aggvar in `list' {
	
	preserve

		* restrict to specific aggregate level
		keep if `agglevel' == "`aggvar'"

		foreach Type in S V {
		
			if "`Type'" == "S" {
				local var PropSqm
			}
			else if "`Type'" == "V" {
				local var PHat`year' 
			}
			
			* drop buildigns constructed after year of reference
			drop if YearConst > `year'
	
			* inequality estimates
			cap ineqdec0 `var'
			cap g `Type'gini = r(gini)
			cap g `Type'p95 = r(p95)
			cap g `Type'p90 = r(p90)
			cap g `Type'p75 = r(p75)
			cap g `Type'p50 = r(p50)
			cap g `Type'p25 = r(p25)
			cap g `Type'p10 = r(p10)
			cap g `Type'p5 = r(p5)
	
		} 

		* one observation per agg level
		duplicates drop CMUN `agglevel', force

		* keep new variables
		keep CMUN `agglevel' *gini *p*
		
		* drop other variables
		drop PropSqm
		
		* year
		g Year = `year'
	
		* store
		compress
		save "$data/temp/Ineq_08_900_`agglevel'`aggvar'`year'.dta", replace
	
	restore

	} 
} 
} 

* put together
foreach agglevel in CMUN CDIS CUSEC BARRI ZIPCode {

	preserve
	
	* retrieve list of values in agglevel
	levelsof `agglevel', local(list)
	
	clear

	forval year = 2019(-1)2009 {	
	
	foreach aggvar in `list' {
	
		* append
		append using "$data/temp/Ineq_08_900_`agglevel'`aggvar'`year'.dta"
		cap erase "$data/temp/Ineq_08_900_`agglevel'`aggvar'`year'.dta"
	
	} 
	} 
	
	* generate indictor for agg level
	g agglevel = "`agglevel'"
	
	if "`agglevel'" == "CUSEC" {
		g CDIS = substr(CUSEC,4,2)
		g CSEC = substr(CUSEC,6,3)
	
		* fix
		replace CUSEC = "08" + CUSEC
		sort CUSEC Year
		order agglevel CMUN CDIS CUSEC Year
	}

	* store
	compress
	save "$data/int/Ineq_08_900_`agglevel'.dta", replace

	restore

} 

* one dataset
clear
append using "$data/int/Ineq_08_900_CMUN.dta"
append using "$data/int/Ineq_08_900_CDIS.dta"
append using "$data/int/Ineq_08_900_BARRI.dta"
append using "$data/int/Ineq_08_900_ZIPCode.dta"
append using "$data/int/Ineq_08_900_CUSEC.dta"

replace CMUN = "" if CDIS!="" | BARRI != "" | ZIPCode != "" | CUSEC != ""

* store
order agglevel CMUN CDIS BARRI ZIPCode CUSEC
compress
save "$data/int/Ineq_08_900_agglevel.dta", replace

}
********************************************************************************
* LNG value/space
********************************************************************************
{
* LNG data
use "$data/int/lng_bcn.dta", clear

* number of apartments in parcel
merge m:1 PlotCode using "$data/int/catastro_plot_chars.dta", keepusing(NApts CPRO CMUN CDIS CSEC BARRI)
drop if _merge==2
drop _merge

* drop individual level vars
drop PlotCode PlotID

* tract id
g CUSEC = CPRO + CMUN + CDIS + CSEC

* label
la var CUSEC "CUSEC"
la var CMUN "Municipality Code"
la var CPRO "Province Code"
la var CDIS "District Code"
la var CSEC "Census Tract Code"

** aggregate
local varlist

foreach type in V S {
foreach d in 100 200 350 500 750 1000 {
	
	#d;
	local varlist `varlist' 
	`type'LNG`d' W`type'LNG`d' `type'p01`d' `type'p05`d' `type'p10`d' 
	`type'p25`d' `type'p50`d' `type'p75`d' `type'p90`d' 
	`type'p95`d' `type'p99`d' `type'Mean`d'
	;
	#d cr
	
}
}

* generate aggregates
foreach agglevel in CMUN CDIS BARRI CUSEC {

	preserve
	
	g x = 1

	foreach var in `varlist' {

		* gen vars
		bys `agglevel' Year: egen `var'_wmean = wtmean(`var'), weight(NApts)
		bys `agglevel' Year: egen `var'_mean = mean(`var')
		bys `agglevel' Year: egen `var'_min = min(`var')
		bys `agglevel' Year: egen `var'_max = max(`var')
		bys `agglevel' Year: egen `var'_med = median(`var')
		bys `agglevel' Year: egen `var'_sd = sd(`var')

		* label
		la var `var'_mean "`var' in `agglevel' (mean)"
		la var `var'_min "`var' in `agglevel' (min)"
		la var `var'_max "`var' in `agglevel' (max)"
		la var `var'_med "`var' in `agglevel' (median)"
		la var `var'_sd "`var' in `agglevel' (std dev)"
		
		drop `var'
	
	} 
	
	* number of observations
	bys `agglevel' Year: egen N = sum(x)
	la var N "Observations in `agglevel'-Year"
	drop x
	* effective N
	bys `agglevel' Year: egen NW = sum(NApts)
	la var NW "Observations in `agglevel'-Year (Weighted)"
	drop NApts

		* keep one observation per agg level
		duplicates drop `agglevel' Year, force
		
		if "`agglevel'" == "CMUN" {
			order CMUN Year N
			drop CDIS BARRI CUSEC CSEC
		}
		else if "`agglevel'" == "CDIS" {
			order CMUN CDIS Year N
			drop BARRI CUSEC CSEC
		}
		else if "`agglevel'" == "BARRI" {
			order CMUN CDIS BARRI Year N
			drop CUSEC CSEC
		}
		else if "`agglevel'" == "CUSEC" {
			order CMUN CDIS CSEC CUSEC Year N
			drop BARRI
		}
		
		* store
		compress
		save "$data/int/LNG_08_900_`agglevel'.dta", replace
	
	restore

} 

}
********************************************************************************
* changes in LNG parcel level
********************************************************************************
{
* load lng data
use "$data/int/lng_bcn.dta", clear	

* make plotcode integer to set panel
egen plotcode = group(PlotCode)

* set panel
xtset plotcode Year
sort plotcode Year

* compute change in LNG
forval lag = 1(1)4 {

	#d;
	foreach var in VLNG100 VLNG200 VLNG350 VLNG500 SLNG100 SLNG200 SLNG350 SLNG500 {
	;
	#d cr
	
	bys plotcode: g D`lag'_`var' = (`var' - l`lag'.`var')
	bys plotcode: g ChngD`lag'_`var' = (`var' - l`lag'.`var')/(l`lag'.`var')
		
	}
}

* keep new vars
keep PlotCode Year D*_* ChngD*
	
* store
compress
save "$data/int/IneqChng_08_900_PlotCode.dta", replace

}
********************************************************************************
* changes in LNG tract level
********************************************************************************
{
* data tract level
use "$data/int/Ineq_08_900_CUSEC.dta", clear	

* add LNG
merge 1:1 CUSEC Year using "$data/int/LNG_08_900_CUSEC.dta", keepusing(W*LNG*_max W*LNG*_med *LNG*_max *LNG*_med *LNG*_wmean *LNG*_sd W*LNG*_wmean W*LNG*_sd)
drop if _m==2
drop _m

* make CUSEC integer
egen cusec = group(CUSEC)

* set panel
xtset cusec Year
sort cusec Year

* compute change in LNG
local varlist

foreach type in V S {
foreach d in 100 200 350 500 {
	
	#d;
	local varlist `varlist' `type'LNG`d'_wmean `type'LNG`d'_max `type'LNG`d'_med
	W`type'LNG`d'_wmean W`type'LNG`d'_max W`type'LNG`d'_med
	;
	#d cr
	
}
}

forval lag = 1(1)4 {

	* space
	g D`lag'_Sgini = Sgini - l`lag'.Sgini
	g ChngD`lag'_Sgini = (Sgini - l`lag'.Sgini)/(l`lag'.Sgini)
	
	* value
	g D`lag'_Vgini = Vgini - l`lag'.Vgini
	g ChngD`lag'_Vgini = (Vgini - l`lag'.Vgini)/(l`lag'.Vgini)
	
	foreach var in `varlist' {
	
		g D`lag'_`var' = `var' - l`lag'.`var'
		g ChngD`lag'_`var' = (`var' - l`lag'.`var')/(l`lag'.`var')
		
	}
}

* keep new vars
keep CMUN CDIS CSEC CUSEC Year D*_* ChngD*

* store
compress
save "$data/int/IneqChng_08_900_CUSEC.dta", replace
}
********************************************************************************
* closing
********************************************************************************
{

cap log close
clear all

}
